<?php

namespace app\sysuser\controller\student;

use app\common\controller\SysBackend;
use app\common\model\banxi\student\Student as StudentModel;
use think\db\exception\PDOException;
use think\exception\ValidateException;
use think\facade\Validate;
use think\validate\ValidateRule;


/**
 * 学员
 */
class Student extends SysBackend
{
    protected array $noNeedLogin = [];

    protected array $noNeedPermission = ['*'];

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new StudentModel();
    }


    /**
     * 分页查询
     */
    public function listPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query",]);
        $where = get_query($param);
        $pageParams = get_page_params($param);
        $list = $this->model->getListPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 分页Select查询
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function listSelectPage(): void
    {
        $param = $this->request->param(['index', 'limit', 'orderBy', "query",'searchText']);
        if (!empty($param['searchText'])){
            $param['query']['LIKE_name'] = $param['searchText'];
        }
        $where = get_query($param);
        $pageParams = get_page_params($param);
        $list = $this->model->getListSelectPage($where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 分页Select课时查询
     * @return void
     * @throws \think\db\exception\DbException
     */
    public function listSelectClassHourPage(): void
    {
        $param = $this->request->param(["index" => 1, 'limit' => 10, "query" => [], "courseFullType" => 0, 'searchText' => '']);
        try {
            $validate = Validate::rule([
                'courseFullType' => ValidateRule::in([1, 2, 3], '请选择正确的课程类型'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        if (!empty($param['searchText'])) {
            $param['query']['LIKE_name'] = $param['searchText'];
        }
        $where = get_query($param);
        $pageParams = get_page_params($param);
        $list = $this->model->getListSelectClassHourPage($param['courseFullType'], $where, $pageParams['page'], $pageParams['limit']);
        $this->ok('操作成功', result_row($list));
    }

    /**
     * 查询
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function get(): void
    {
        $param = $this->request->param(["query" => []]);
        if (empty($param['query'])) {
            $this->fail('请传递你要查询的记录');
        }
        $where = get_query($param);
        $info = $this->model->getInfo($where);
        if (empty($info)) {
            $this->fail('记录不存在');
        }
        $this->ok('操作成功', $info);
    }


    /**
     * 新增或修改
     * @return void
     */
    public function saveOrUpdate(): void
    {
        $param = $this->request->filter('trim')->param();
        if (empty($param)) {
            $this->ok('请传入参数');
        }
        try {
            $this->model->saveOrUpdate($param);
        } catch (ValidateException|PDOException|\Exception $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok('操作成功', [], self::NOTIFICATION);
    }

    /**
     * 启停用
     * @return void
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function enable(): void
    {
        $param = $this->request->param(['id' => 0]);
        try {
            $validate = Validate::rule([
                'id' => ValidateRule::isRequire(null, '请选择你要修改的学生'),
            ]);
            if (!$validate->check($param)) throw new ValidateException($validate->getError());
            $this->model->updateEnable($param['id']);
        } catch (ValidateException $exception) {
            $this->fail($exception->getMessage());
        }
        $this->ok();
    }
}